/**
 * 
 */
package de.wesemann.mediahandbook.le.listener;

import java.io.FileNotFoundException;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import de.wesemann.jdedun.entities.mediahandbook.branches.MediahandbookBranchesContainer;
import de.wesemann.jdedun.exceptions.PageNotFoundException;
import de.wesemann.mediahandbook.le.adapter.BranchAdapter;
import de.wesemann.mediahandbook.le.util.BranchUtil;

/**
 * @author spinner0815
 * 
 */
public class BranchOverviewEndlessScrollListener implements OnScrollListener {

	private int visibleThreshold = 5;
	private int previousTotal = 0;
	private boolean loading = true;
	private BranchAdapter mBranchAdap = null;
	private boolean endReached = false;
	private static final String LOG_TAG = BranchOverviewEndlessScrollListener.class.getSimpleName();
	private MediahandbookBranchesContainer mBranchContainer = null;
	private ProgressBar progressBar;

	public BranchOverviewEndlessScrollListener(int visibleThreshold, BranchAdapter branchAdap, ListView list,
			MediahandbookBranchesContainer branchContainer, ProgressBar progressBar) {
		this.mBranchAdap = branchAdap;
		this.progressBar = progressBar;
		// this.list = list;
		// this.footerView = footerView;
		this.visibleThreshold = visibleThreshold;
		this.mBranchContainer = branchContainer;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (loading && !endReached) {
			if (totalItemCount > previousTotal) {
				loading = false;
				previousTotal = totalItemCount;

			}
		}
		if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
			Log.d(LOG_TAG, "it should be loading now");
			// I load the next page of gigs using a background task,
			// but you can call any function here.
			new LoadItems().execute();
			loading = true;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
	}

	private class LoadItems extends AsyncTask<Void, Void, Void> {
		// private List<MediahandbookCompany> companies;

		@Override
		protected Void doInBackground(Void... params) {

			try {
				if (mBranchContainer.getPaging() == null) {
					endReached = true;
				} else {
					try {
						mBranchContainer = BranchUtil.getNextBranchs(mBranchContainer.getPaging());
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			} catch (PageNotFoundException e) {
				// e.printStackTrace();
				endReached = true;
				this.cancel(true);
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Log.d(LOG_TAG, "PRE Executing");
			progressBar.setVisibility(View.VISIBLE);
			// if (list.getFooterViewsCount() <= 0) {
			// Log.d(LOG_TAG, "not footers found");
			// list.addFooterView(footerView);
			// }

		}

		@Override
		protected void onPostExecute(Void result) {
			// if (list.getFooterViewsCount() > 0) {
			//
			//
			// }
			if (!endReached && mBranchContainer.getData() != null && !mBranchContainer.getData().isEmpty()) {
				// if (list.getFooterViewsCount() > 0) {
				// list.removeFooterView(footerView);
				// }
				Log.d(LOG_TAG, "number fo results " + mBranchContainer.getData().size());
				mBranchAdap.addItems(mBranchContainer.getData());
			}
			progressBar.setVisibility(View.INVISIBLE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			Log.d(LOG_TAG, "AsyncTask for EndlessScrollListener canceled!");
			mBranchContainer = null;
			// if (list.getFooterViewsCount() != 0) {
			// list.removeFooterView(footerView);
			// }
		}

	}
}
